﻿using System.Collections.Generic;
using System.Linq;
using ZilLion.Service.Core.DapperWrapper;
using ZilLion.Service.Core.DapperWrapper.SqlServer;
using ZilLion.Service.WeChatMall.Domain.Entitys.Orders;
using ZilLion.Service.WeChatMall.Domain.Repositorys.Interface.Order;

namespace ZilLion.Service.WeChatMall.Domain.Repositorys.Order
{
    public class PrePayOrderSinceDeliveryRepository : SqlServerRespository<PrePayOrderDeliverySinceEntity>,
        IPrePayOrderSinceInfoRepository
    {
        public PrePayOrderSinceDeliveryRepository()
        {
            Context = new WechatMallContext();
        }

        public PrePayOrderDeliverySinceEntity GetOrderSinceDeliveryInfo(string sno)
        {
            var querysql = DapperHelper.CreateColSqlByMap<PrePayOrderDeliverySinceEntity>();
            var sql =
                $@"SELECT {querysql}
                FROM    weiscdzfddjfxxt                
                where ddlsh =@sno";
            return GetList(sql, new {sno}).FirstOrDefault();
        }

        public IList<PrePayOrderDeliverySinceEntity> GetOrderSinceDeliveryInfos(IList<string> snolist)
        {
            var condition = snolist.Aggregate(string.Empty, (current, sno) => current + sno + ",");
            if (condition.EndsWith(","))
                condition = condition.Left(condition.Length - 1);
            var querysql = DapperHelper.CreateColSqlByMap<PrePayOrderDeliverySinceEntity>();
            var sql =
                $@"SELECT  {querysql}
                FROM    weiscdzfddjfxxt                
                where ddlsh in ('{condition}')";
            return GetList(sql).ToList();
        }

        public void SaveOrderSinceDelivery(PrePayOrderDeliverySinceEntity entity, OrderMainInfoDto orderMain,
            UnitOfWork unit = null)
        {
            DapperHelper.Execute("delete from weiscdzfddjfxxt where ddlsh=@sno", Context, unit,
                new {sno = orderMain.Sno});
            Add(entity, unit);
        }

        public void RemoveBatch(string sno, UnitOfWork unit = null)
        {
            DapperHelper.Execute("delete from weiscdzfddjfxxt where ddlsh=@sno", Context, unit,
                new {sno});
        }
    }
}